package com.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.dao.*;
import com.model.*;
import com.sun.org.apache.regexp.internal.RE;
import com.tx.PgParam.PgParamO;
import com.tx.PgParam.TMIqcbhO;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.icim.service.ICIMBaseService;
import com.mapKeyVal.key.StatBoxOpeBnkKey;
import com.mapKeyVal.key.StatBoxOpeBnkWoKey;
import com.tx.Xpcrtno.XpcrtnoI;
import com.tx.Xpcrtno.XpcrtnoO;
import com.tx.Xpinqlot.XpinqlotI;
import com.tx.Xpinqlot.XpinqlotO;
import com.tx.Xpinqlot.XpinqlotOA;
import com.tx.Xpinqlot.XpinqlotOB;
import com.util.MqOpe;
import com.util.Util;

import static com.def.GenericDef.*;

@Scope("prototype")
@Service("XpinqlotService")
public class XpinqlotService extends ICIMBaseService {
    private static Logger logger = Logger.getLogger(XpinqlotService.class);
    private static String TX_NAME = "XPINQLOT";

    private XpinqlotI inTrx;
    private XpinqlotO outTrx;

    @Autowired
    private IRetPrdInfoDao retPrdInfoDao;

    @Autowired
    private IRetPrdLotDao retPrdLotDao;

    @Autowired
    private IRetWODao retWoDao;
    @Autowired
    private IRetBoxDao retBoxDao;
    @Autowired
    private IBisMdlDefDao bisMdlDefDao;

    @Autowired
    private IBisLayotDao bisLayotDao;

    @Autowired
    private IBisToolDao bisToolDao;
    @Autowired
    private IRetPrdInDao retPrdInDao;
    @Autowired
    private XpcrtnoService xpcrtnoService;

    @Autowired
    private IBisPathiDao bisPathiDao;
    @Autowired
    private IBisPathDao bisPathDao;

    @Autowired

    private ISpcRetMfgDataDao spcRetMfgDataDao;


    private String gszMsgBuf = "";
    private Timestamp gEvtTimestamp;
    private Double gEvtTimeD;

    public String subMainProc(String strInMsg) {
        long rtnCode = _NORMAL;
        gszMsgBuf = "";
        gEvtTimestamp = Util.getCurrentTimestamp();
        gEvtTimeD = Util.getDoubleTime();
        String outMsg = "";
        inTrx = new XpinqlotI();
        outTrx = new XpinqlotO();
        outTrx.setTrx_id(TX_NAME);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

        MqOpe mqOpe = new MqOpe();

        try {
            inTrx = (XpinqlotI) mqOpe.inStr2Obj(strInMsg, inTrx);
            logger.info("InTrx is " + strInMsg);

            rtnCode = subMainProc2(inTrx.getAction_flg().toCharArray()[0]);
            if (rtnCode != _NORMAL) {
                outTrx.setRtn_code(String.valueOf(rtnCode));
                outTrx.setRtn_mesg(gszMsgBuf);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            } else {
                outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
                outTrx.setRtn_mesg(RETURN_MESG_OK);
                retPrdInfoDao.flushCurSession();
            }
        } catch (Exception ex) {
            outTrx.setRtn_mesg(ex.toString());
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            logger.error(Util.stackTraceToString(ex));
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            outMsg = mqOpe.obj2Str(outTrx);
            logger.info(outMsg);
        }
        return outMsg;
    }

    private long subMainProc2(char actionType) {
        long rtnCode = _NORMAL;

        switch (actionType) {
            case 'Q': {
                rtnCode = getLotInfo();
                break;
            }
            case 'L': {
                rtnCode = getPrdListInfo();
                break;
            }
            case 'C': {
                rtnCode = creatLotId();
                break;
            }
            case 'M': {
                rtnCode = getPrdListInfo();
                if (rtnCode != 0) {
                    break;
                }
                rtnCode = getSummaryInfoForLot();
                break;
            }
            case 'G': {
                rtnCode=getPrdPgParam();
                    break;
            }case 'S' :{
                rtnCode =savePGflg();
                break;
            }
            default: {
                gszMsgBuf = "Invalide type input: [" + actionType + "]";
                return E_XPINQBOX_INVALID_INPUT;
            }
        }
        return rtnCode;
    }


    private long getLotInfo() {
        return 0;
    }

    private long getPrdListInfo() {

        long lRc = _NORMAL;
        List<Ret_prd_info> retPrdInfoList = null;
        List<XpinqlotOA> oaryList = new ArrayList<XpinqlotOA>();
        if (inTrx.getProc_flg() == null) {
            String hql = " FROM Ret_prd_info where lot_id=:lot_id";
            try {
                Query query = retPrdInfoDao.getCrSession().createQuery(hql);
                query.setString("lot_id", inTrx.getLot_id());
                retPrdInfoList = query.list();
            } catch (Exception ex) {
                lRc = E_XPINQLOT_OTHER_ERROR;
                logger.error("read RET_PRD_INFO ERROR:" + ex.getMessage());
                logger.error(Util.stackTraceToString(ex));
            }
        } else if (inTrx.getProc_flg().equals("JB")) {
            String hql = " FROM Ret_prd_info where fst_lot_id_fk=:fst_lot_id";
            try {
                Query query = retPrdInfoDao.getCrSession().createQuery(hql);
                query.setString("fst_lot_id", inTrx.getLot_id());
                retPrdInfoList = query.list();
            } catch (Exception ex) {
                lRc = E_XPINQLOT_OTHER_ERROR;
                logger.error("read RET_PRD_INFO ERROR:" + ex.getMessage());
                logger.error(Util.stackTraceToString(ex));
            }
        } else {
            String hql = " FROM Ret_prd_info where snd_lot_id_fk=:snd_lot_id";
            try {
                Query query = retPrdInfoDao.getCrSession().createQuery(hql);
                query.setString("snd_lot_id", inTrx.getLot_id());
                retPrdInfoList = query.list();
            } catch (Exception ex) {
                lRc = E_XPINQLOT_OTHER_ERROR;
                logger.error("read RET_PRD_INFO ERROR:" + ex.getMessage());
                logger.error(Util.stackTraceToString(ex));
            }
        }


        for (Ret_prd_info ret_prd_info : retPrdInfoList) {
            XpinqlotOA oary = new XpinqlotOA();
            oary.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
            oary.setFab_sn(ret_prd_info.getFab_sn());
            oary.setSlot_no(ret_prd_info.getSlot_no());
            oary.setPrd_stat(ret_prd_info.getPrd_stat());
            oary.setBox_id_fk(ret_prd_info.getBox_id_fk());
            oary.setPrd_grade(ret_prd_info.getPrd_grade());
            String pathId = PROC_WAIT.equals(ret_prd_info.getPrd_stat()) ? ret_prd_info.getNx_path_id_fk() : ret_prd_info.getCr_path_id_fk();
            String pathVer = PROC_WAIT.equals(ret_prd_info.getPrd_stat()) ? ret_prd_info.getNx_path_ver_fk() : ret_prd_info.getCr_path_ver_fk();
            //增加返工品标识
//			if(!Util.isSpaceCheck(ret_prd_info.getOqc_skip_flg())){
//				oary.setOqc_skip_flg(ret_prd_info.getOqc_skip_flg());
//			}
            Bis_pathiId id = new Bis_pathiId();
            id.setPath_id(pathId);
            id.setPath_ver(pathVer);
            Bis_pathi bis_pathi = bisPathiDao.get(id);
            if (bis_pathi != null) {
                if (bis_pathi.getPath_cate().equals(PATH_TYPE_REWK)) {
                    oary.setOqc_skip_flg(_YES);
                } else {
                    oary.setOqc_skip_flg(_NO);
                }
            }
            oary.setBnk_flg(ret_prd_info.getBnk_flg());
            if (ret_prd_info.getPrd_stat().equals("INPR")) {
                oary.setCr_ope_id(ret_prd_info.getCr_ope_id_fk());
                oary.setCr_ope_no(ret_prd_info.getCr_ope_no_fk());
                oary.setCr_ope_ver(ret_prd_info.getCr_ope_ver_fk());
                oary.setCr_path_id(ret_prd_info.getCr_path_id_fk());
                oary.setCr_path_ver(ret_prd_info.getCr_path_ver_fk());
                oary.setCr_proc_id_fk(ret_prd_info.getCr_proc_id_fk());
            } else {
                oary.setCr_ope_id(ret_prd_info.getNx_ope_id_fk());
                oary.setCr_ope_no(ret_prd_info.getNx_ope_no_fk());
                oary.setCr_ope_ver(ret_prd_info.getNx_ope_ver_fk());
                oary.setCr_path_id(ret_prd_info.getNx_path_id_fk());
                oary.setCr_path_ver(ret_prd_info.getNx_path_ver_fk());
                oary.setCr_proc_id_fk(ret_prd_info.getNx_proc_id_fk());
            }
            oary.setTool_id_fk(ret_prd_info.getTool_id_fk());
            oary.setLot_id_fk(ret_prd_info.getLot_id());
            oary.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
            oary.setSo_id_fk(ret_prd_info.getSo_id_fk());
            oary.setWo_id_fk(ret_prd_info.getWo_id_fk());
            oary.setMtrl_box_id(ret_prd_info.getMtrl_box_id_fk());
            oaryList.add(oary);
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        outTrx.setOary(oaryList);
        outTrx.setSht_cnt(oaryList.size());

        return lRc;

    }

    private long creatLotId() {
        long lRc = _NORMAL;
        MqOpe mqOpe = new MqOpe();
//		String lot_id = "L";
        String lot_id = "";
        String key_id = "L";
        String lot_data = "";
        String lot_banbie = "";

        //获取设备信息
        /*Bis_tool bis_tool = bisToolDao.get(inTrx.getTool_id());
        if (bis_tool == null) {
			gszMsgBuf = "未找到设备代码:[" + inTrx.getTool_id() + "]!";
			return E_BIS_TOOL + E_READ_NOT_FOUND;
		}*/
        //根据设备区域代码，确定批次第一码厂别。F代表1厂；S代表2厂；T代表3厂
//		if (bis_tool.getBay_id().equals("F2")) {
//			lot_id = "S";
//			key_id = "S"; 
//		} else if (bis_tool.getBay_id().equals("M3")) {
//			lot_id = "T";
//			key_id = "T";
//		} else {
//			gszMsgBuf = "请检查设备代码:[" + inTrx.getTool_id() + "]是否为来料检设备！并且设备区域代码是否为F2或M3";
//			return E_XPINQLOT_ERROR_TOOL_ID_OR_BAY_ID;
//		}
        //获取工单信息
        Ret_wo ret_wo = retWoDao.get(inTrx.getWo_id());
        if (ret_wo == null) {
            gszMsgBuf = "未找到工单代码:[" + inTrx.getWo_id() + "]!";
            return E_RET_WO + E_READ_NOT_FOUND;
        }
        lot_id += inTrx.getWo_id();
	/*	//获取产品信息
		Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(ret_wo.getMdl_id_fk());
		if (bis_mdl_def == null) {
			gszMsgBuf = "未找到产品代码:[" + ret_wo.getMdl_id_fk() + "]!";
			return E_BIS_MDL_DEF + E_READ_NOT_FOUND;
		}
		//获取产品layout信息
		Bis_layot bis_layot = (Bis_layot) bisLayotDao.get(bis_mdl_def.getLayot_id_fk());
		if (bis_layot == null) {
			gszMsgBuf = "未找到Layot代码:[" + bis_mdl_def.getLayot_id_fk() + "]!";
			return E_BIS_LAYOT + E_READ_NOT_FOUND;
	    }
	   //根据产品layout的版式规格确定批次第二码。版式规格为L，第二码为1；S，第二码为2.
	    if(bis_layot.getLayot_cate().equals("L")){
	    	lot_id = lot_id + "1";
	    	key_id = key_id + "1";
	    }else if(bis_layot.getLayot_cate().equals("S")){
	    	lot_id = lot_id + "2";
	    	key_id = key_id + "2";
	    }else{
			gszMsgBuf = "请检查Layot代码:[" + bis_mdl_def.getLayot_id_fk() + "]的版式规格是否为L或S";
			return E_XPINQLOT_ERROR_LAYOT_CATE;
	    }*/
        //获取时间，确定批次第3至8码年月日
	   /* lot_data = Util.getLotData().substring(2,8);
	    lot_id = lot_id + lot_data;
	    key_id = key_id + lot_data;*/
        //根据目标厚度，确定批次第9至12码产品厚度
        //lot_id = lot_id + Util.getThickFormat(ret_wo.getTo_thickness());
        //根据系统时间，确定批次第13码白夜班
        lot_banbie = Util.getShiftType();
        //lot_id = lot_id + lot_banbie;
        key_id = key_id + lot_banbie;
        XpcrtnoI xpcrtNOI = new XpcrtnoI();
        xpcrtNOI.setType("XPCRTNO");
        xpcrtNOI.setTrx_id("I");
        xpcrtNOI.setKey_id(lot_id);
        xpcrtNOI.setType(STRING_SERIAL_TYPE_LOT);
        xpcrtNOI.setAction_flg("L");
        String strInMsg = mqOpe.obj2Str(xpcrtNOI);
        String strOutMsg = xpcrtnoService.subMainProc(strInMsg);
        XpcrtnoO xpcrtNoO = new XpcrtnoO();
        xpcrtNoO = (XpcrtnoO) mqOpe.inStr2Obj(strOutMsg, xpcrtNoO);
        if (!xpcrtNoO.getRtn_code().equals(RETURN_CODE_OK)) {
            gszMsgBuf = xpcrtNoO.getRtn_mesg();
            return Integer.valueOf(xpcrtNoO.getRtn_code());
        } else {
            lot_id = lot_id + "-" + xpcrtNoO.getSerial_no();
        }

        outTrx.setLot_id(lot_id);
        return lRc;
    }
    private long savePGflg(){
        long lRc = _NORMAL;
        String flg= inTrx.getType_flg();
        String box_id=inTrx.getBox_id();
        String evt_usr=inTrx.getEvt_usr();
        Ret_box ret_box = retBoxDao.get(box_id);
        if(ret_box ==null){
            gszMsgBuf="箱号信息不存在["+box_id+"]";
            return E_RET_BOX;
        }
        String sql = "from Ret_prd_info where mtrl_box_id_fk ='" + box_id + "'";
        List<Ret_prd_info> retPrdInfos = retPrdInfoDao.find(sql);
        String ope_flg="Y";
        for (Ret_prd_info retPrdInfo :retPrdInfos){
            if(!"WAIT".equals(retPrdInfo.getPrd_stat())||!"UNT-000BZ".equals(retPrdInfo.getNx_ope_id_fk())){
                ope_flg="N";
             break;
            }
        }
        if("N".equals(ope_flg)){
            gszMsgBuf="请确认该来料箱["+box_id+"]所有玻璃为WAIT，且在包装站点才能确认数据";
            return E_RET_PRD_INFO;
        }
        if("PGCS".equals(flg)) {
            ret_box.setExt_17("Y");
        }else if("IQC".equals(flg)){
            ret_box.setExt_18("Y");
        }else if("CJ".equals(flg)){
            ret_box.setExt_19("Y");
        }else if("DM".equals(flg)){
            ret_box.setExt_20("Y");
        }
        ret_box.setEvt_usr(evt_usr);
        retBoxDao.update(ret_box);
        return  lRc;
    }


    private long getPrdPgParam() {
        long lRc = _NORMAL;
        String box_id = inTrx.getBox_id();
        String type_flg = inTrx.getType_flg();
        Ret_box ret_box = retBoxDao.get(box_id);
        if(ret_box==null){
            gszMsgBuf="该箱号不存在请确认!!!!!";
            return E_RET_BOX ;
        }
        Ret_prd_inId ret_prd_inId =new Ret_prd_inId();
        ret_prd_inId.setCus_id("006");
        ret_prd_inId.setMtrl_box_id(box_id);
        Ret_prd_in ret_prd_in = retPrdInDao.get(ret_prd_inId);
        if(ret_prd_in==null){
            gszMsgBuf="该来料箱号信息不存在请确认!!!!!";
            return E_RET_PRD_IN;
        }
        Ret_wo ret_wo = retWoDao.get(ret_prd_in.getWo_id_fk());
        String sql = "from Ret_prd_info where mtrl_box_id_fk ='" + box_id + "'";
        List<Ret_prd_info> retPrdInfos = retPrdInfoDao.find(sql);
          String sqlforDM="from Bis_path where path_id_fk ='" + ret_wo.getPath_id_fk()+ "' ";
         List <Bis_path> bis_paths =bisPathDao.find(sqlforDM);
         String DMFlg="";
         for(int i=0;i<bis_paths.size();i++){
                 if (bis_paths.get(i).getCr_ope_id_fk().contains("DM")){
                     DMFlg="Y";
                     break;
                 }else{
                     DMFlg="N";
                 }
          }
        List<PgParamO> oaryCList = new ArrayList<PgParamO>();
        List<TMIqcbhO> oaryDList = new ArrayList<TMIqcbhO>();
        for (Ret_prd_info retPrdInfo : retPrdInfos) {
            String PrdSeqId = retPrdInfo.getPrd_seq_id();
            if ("PGCS".equals(type_flg)) {
                String hql = "from Spc_ret_mfg_data where id.prd_seq_id_fk='" + PrdSeqId + "' and tool_id_fk like 'UNT-PGJ%'";
                List<Spc_ret_mfg_data> spc_ret_mfg_dataList = spcRetMfgDataDao.find(hql);
                if (spc_ret_mfg_dataList == null || spc_ret_mfg_dataList.size() == 0) {
                    gszMsgBuf="玻璃[" + PrdSeqId + "]未找到抛光参数信息请确认!!!";
                    return E_SPC_RET_MFG_DATA ;
                } else {
                    Spc_ret_mfg_data spc_ret_mfg_data = spc_ret_mfg_dataList.get(0);
                    PgParamO oaryC = new PgParamO();
                    oaryC.setPrd_seq_id_fk(retPrdInfo.getPrd_seq_id());
                    oaryC.setPg_tool_id_fk(spc_ret_mfg_data.getTool_id_fk());
                    oaryC.setT_rubbing_time(retPrdInfo.getT_rubbing_time());
                    oaryC.setC_rubbing_time(retPrdInfo.getC_rubbing_time());
                    oaryC.setData_value_002(spc_ret_mfg_data.getData_value_002());
                    oaryC.setData_value_003(spc_ret_mfg_data.getData_value_003());
                    oaryC.setData_value_004(spc_ret_mfg_data.getData_value_004());
                    oaryC.setData_value_005(spc_ret_mfg_data.getData_value_005());
                    oaryC.setData_value_006(spc_ret_mfg_data.getData_value_006());
                    oaryC.setRpt_usr(spc_ret_mfg_data.getRpt_usr());
                    oaryCList.add(oaryC);
                }
            } else if ("IQC".equals(type_flg)) {
                String hqlforIQC = "from Spc_ret_mfg_data where id.prd_seq_id_fk='" + PrdSeqId + "' and tool_id_fk = 'IQC'";
                List<Spc_ret_mfg_data> spc_ret_mfg_dataList = spcRetMfgDataDao.find(hqlforIQC);
                if (spc_ret_mfg_dataList == null || spc_ret_mfg_dataList.size() == 0) {
                    gszMsgBuf= "玻璃[" + PrdSeqId + "]未找到IQC板厚信息请确认!!!";
                    return E_SPC_RET_MFG_DATA ;
                } else {
                    Spc_ret_mfg_data spc_ret_mfg_data = spc_ret_mfg_dataList.get(0);
                    TMIqcbhO oaryD = new TMIqcbhO();
                    oaryD.setPrd_seq_id_fk(retPrdInfo.getPrd_seq_id());
                    oaryD.setData_cnt(spc_ret_mfg_data.getData_cnt());
                    oaryD.setData_value_001(spc_ret_mfg_data.getData_value_001());
                    oaryD.setData_value_002(spc_ret_mfg_data.getData_value_002());
                    oaryD.setData_value_003(spc_ret_mfg_data.getData_value_003());
                    oaryD.setData_value_004(spc_ret_mfg_data.getData_value_004());
                    oaryD.setData_value_005(spc_ret_mfg_data.getData_value_005());
                    oaryD.setData_value_006(spc_ret_mfg_data.getData_value_006());
                    oaryD.setData_value_007(spc_ret_mfg_data.getData_value_007());
                    oaryD.setData_value_008(spc_ret_mfg_data.getData_value_008());
                    oaryD.setRpt_usr(spc_ret_mfg_data.getRpt_usr());
                    oaryDList.add(oaryD);
                }

            } else if ("CJ".equals(type_flg)) {
                String hqlforCJ = "from Spc_ret_mfg_data where id.prd_seq_id_fk='" + PrdSeqId + "' and tool_id_fk like 'UNT-CJ%'";
                List<Spc_ret_mfg_data> spc_ret_mfg_dataList = spcRetMfgDataDao.find(hqlforCJ);
                if (spc_ret_mfg_dataList == null || spc_ret_mfg_dataList.size() == 0) {
                   gszMsgBuf="玻璃[" + PrdSeqId + "]未找到成检板厚信息请确认!!!";
                    return E_SPC_RET_MFG_DATA ;
                } else {
                    Spc_ret_mfg_data spc_ret_mfg_data = spc_ret_mfg_dataList.get(0);
                    TMIqcbhO oaryD = new TMIqcbhO();
                    oaryD.setPrd_seq_id_fk(retPrdInfo.getPrd_seq_id());
                    oaryD.setData_cnt(spc_ret_mfg_data.getData_cnt());
                    oaryD.setData_value_001(spc_ret_mfg_data.getData_value_001());
                    oaryD.setData_value_002(spc_ret_mfg_data.getData_value_002());
                    oaryD.setData_value_003(spc_ret_mfg_data.getData_value_003());
                    oaryD.setData_value_004(spc_ret_mfg_data.getData_value_004());
                    oaryD.setData_value_005(spc_ret_mfg_data.getData_value_005());
                    oaryD.setData_value_006(spc_ret_mfg_data.getData_value_006());
                    oaryD.setData_value_007(spc_ret_mfg_data.getData_value_007());
                    oaryD.setData_value_008(spc_ret_mfg_data.getData_value_008());
                    oaryD.setData_value_009(spc_ret_mfg_data.getData_value_009());
                    oaryD.setRpt_usr(spc_ret_mfg_data.getRpt_usr());
                    oaryDList.add(oaryD);
                }
            }else{
                if("Y".equals(DMFlg)) {
                    String hqlforDM = "from Spc_ret_mfg_data where id.prd_seq_id_fk='" + PrdSeqId + "' and tool_id_fk like 'UNT-DM%'";
                    List<Spc_ret_mfg_data> spc_ret_mfg_dataList = spcRetMfgDataDao.find(hqlforDM);
                    if (spc_ret_mfg_dataList == null || spc_ret_mfg_dataList.size() == 0) {

                       gszMsgBuf="玻璃[" + PrdSeqId + "]未找到成检板厚信息请确认!!!";
                        return E_SPC_RET_MFG_DATA ;
                    } else {
                        Spc_ret_mfg_data spc_ret_mfg_data = spc_ret_mfg_dataList.get(0);
                        TMIqcbhO oaryD = new TMIqcbhO();
                        oaryD.setPrd_seq_id_fk(retPrdInfo.getPrd_seq_id());
                        oaryD.setData_cnt(spc_ret_mfg_data.getData_cnt());
                        oaryD.setData_value_001(spc_ret_mfg_data.getData_value_001());
                        oaryD.setData_value_002(spc_ret_mfg_data.getData_value_002());
                        oaryD.setData_value_003(spc_ret_mfg_data.getData_value_003());
                        oaryD.setData_value_004(spc_ret_mfg_data.getData_value_004());
                        oaryD.setData_value_005(spc_ret_mfg_data.getData_value_005());
                        oaryD.setData_value_006(spc_ret_mfg_data.getData_value_006());
                        oaryD.setData_value_007(spc_ret_mfg_data.getData_value_007());
                        oaryD.setData_value_008(spc_ret_mfg_data.getData_value_008());
                        oaryD.setData_value_009(spc_ret_mfg_data.getData_value_009());
                        oaryD.setRpt_usr(spc_ret_mfg_data.getRpt_usr());
                        oaryDList.add(oaryD);
                    }
                }else{
                    gszMsgBuf="此工单工艺路线无镀膜信息无需确认!!!!!!";
                    return lRc;
                }
            }
        }
        outTrx.setOaryC(oaryCList);
        outTrx.setOaryD(oaryDList);
         return lRc;
             }


    /**
     * Add lot info
     *
     * @return
     */
    private long addLotInfo(String lot_id, Integer prd_qty) {
        Ret_prd_lot ret_prd_lot = new Ret_prd_lot();
        Ret_prd_lotId ret_prd_lotId = new Ret_prd_lotId();
        ret_prd_lotId.setLot_id(lot_id);
        ret_prd_lotId.setLot_splt_id(LOT_SPLIT_0);

        ret_prd_lot = retPrdLotDao.get(ret_prd_lotId);
        if (null != ret_prd_lot) {
            ret_prd_lot.setLot_qty(prd_qty + ret_prd_lot.getLot_qty());
            ret_prd_lot.setEvt_usr(inTrx.getEvt_usr());
            ret_prd_lot.setEvt_timestamp(gEvtTimestamp);
            ret_prd_lot.setEvt_time_d(gEvtTimeD);
            retPrdLotDao.update(ret_prd_lot);
        } else {
            ret_prd_lot = new Ret_prd_lot();
            ret_prd_lotId = new Ret_prd_lotId();
            ret_prd_lotId.setLot_id(lot_id);
            ret_prd_lotId.setLot_splt_id(LOT_SPLIT_0);
            ret_prd_lot.setId(ret_prd_lotId);
            ret_prd_lot.setLot_stat(PROC_WAIT);
            ret_prd_lot.setLot_qty(prd_qty);
            ret_prd_lot.setEvt_usr(inTrx.getEvt_usr());
            ret_prd_lot.setEvt_timestamp(gEvtTimestamp);
            ret_prd_lot.setEvt_time_d(gEvtTimeD);
            ret_prd_lot.setStb_timestamp(gEvtTimestamp);

            retPrdLotDao.save(ret_prd_lot);
        }

        return _NORMAL;
    }

    private long getSummaryInfoForLot() {
        String lotId = inTrx.getLot_id();
        int lotTotalCnt = 0, finishedCnt = 0, unFinishedCnt = 0;
        int logofCnt;
        String opeId, prdStat, cusId = "", soId = "", woId = "", mdlId = "", oqc_skip_flg = " ";
//		Map<CompPrd, Integer> compPrdMap = new HashMap<CompPrd, Integer>();
        Map<StatBoxOpeBnkKey, Integer> wipMap = new HashMap<StatBoxOpeBnkKey, Integer>();
        Map<StatBoxOpeBnkKey, String> oqcskpflgMap = new HashMap<StatBoxOpeBnkKey, String>();
        Map<String, Integer> opeUnLogofMap = new HashMap<String, Integer>();
        Map<String, Integer> opeLogofMap = new HashMap<String, Integer>();
//		String hql = "FROM Ret_prd_lot WHERE lot_id = :lot_id";
//		Query query = retPrdLotDao.getCrSession().createQuery(hql);
//		query.setString("lot_id", lotId);
//		Ret_prd_lot retPrdLot = (Ret_prd_lot) query.uniqueResult();
//		if (retPrdLot == null) {
//			gszMsgBuf = "查不到批次信息";
//			return 1;
//		}
        String hql = "";
        if (inTrx.getProc_flg().equals("JB")) {
            hql = "FROM Ret_prd_info WHERE fst_lot_id_fk =? ";
        } else {
            hql = "FROM Ret_prd_info WHERE snd_lot_id_fk =? ";
        }
        List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.list(hql, lotId);
        if (retPrdInfoList.isEmpty()) {
            gszMsgBuf = "查不到批次信息";
            return 1;
        }
        woId = retPrdInfoList.get(0).getWo_id_fk();
        if (StringUtils.isEmpty(woId)) {
            gszMsgBuf = "玻璃[" + retPrdInfoList.get(0).getPrd_seq_id() + "]的工单信息为空,错误";
            return 2;
        }
        Ret_wo ret_wo = retWoDao.get(woId);
        if (ret_wo == null) {
            gszMsgBuf = "找不到内部订单信息";
            return 3;
        }
        cusId = ret_wo.getCus_id_fk();
        soId = ret_wo.getSo_id();
        mdlId = ret_wo.getMdl_id_fk();

        for (Ret_prd_info retPrdInfo : retPrdInfoList) {
            //统计批次总数量,成品数量,半成品数量
            if ("SHIP".equals(retPrdInfo.getPrd_stat()) && "1".equals(retPrdInfo.getBnk_flg())) {
                finishedCnt++;
            } else {
                unFinishedCnt++;
            }
            lotTotalCnt++;
            //统计在制数量
            //统计INPR产品
            opeId = "INPR".equals(retPrdInfo.getPrd_stat()) ? retPrdInfo.getCr_ope_id_fk() : retPrdInfo.getNx_ope_id_fk();
            prdStat = retPrdInfo.getPrd_stat();
            String pathId = PROC_WAIT.equals(retPrdInfo.getPrd_stat()) ? retPrdInfo.getNx_path_id_fk() : retPrdInfo.getCr_path_id_fk();
            String pathVer = PROC_WAIT.equals(retPrdInfo.getPrd_stat()) ? retPrdInfo.getNx_path_ver_fk() : retPrdInfo.getCr_path_ver_fk();
            StatBoxOpeBnkKey wipKey = new StatBoxOpeBnkKey(prdStat, opeId, retPrdInfo.getBox_id_fk(), retPrdInfo.getBnk_flg());

            if (wipMap.containsKey(wipKey)) {
                wipMap.put(wipKey, wipMap.get(wipKey) + 1);
            } else {
                wipMap.put(wipKey, 1);
            }
            //增加返工品标识
            Bis_pathiId id = new Bis_pathiId();
            id.setPath_id(pathId);
            id.setPath_ver(pathVer);
            Bis_pathi bis_pathi = bisPathiDao.get(id);
            if (bis_pathi != null) {
                if (bis_pathi.getPath_cate().equals(PATH_TYPE_REWK)) {
                    oqc_skip_flg = "Y";
                } else {
                    oqc_skip_flg = "N";
                }
            }
//			if(!Util.isSpaceCheck(retPrdInfo.getOqc_skip_flg())){
//				oqc_skip_flg = retPrdInfo.getOqc_skip_flg();
//			}
            if (oqcskpflgMap.containsKey(wipKey)) {
                if (oqc_skip_flg.equals("Y")) {
                    oqcskpflgMap.put(wipKey, oqc_skip_flg);
                } else if (oqc_skip_flg.equals("N") && oqcskpflgMap.get(wipKey).equals(" ")) {
                    oqcskpflgMap.put(wipKey, oqc_skip_flg);
                }
                ;
            } else {
                oqcskpflgMap.put(wipKey, oqc_skip_flg);
            }

            //统计站点数量
            if (opeUnLogofMap.containsKey(opeId)) {
                opeUnLogofMap.put(opeId, opeUnLogofMap.get(opeId) + 1);
            } else {
                opeUnLogofMap.put(opeId, 1);
            }


        }
        Iterator iter = opeUnLogofMap.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<String, Integer> entry = (Entry<String, Integer>) iter.next();
            logger.info(entry.getKey() + ":" + entry.getValue());
            //统计该站已完成数量 TODO:HIS_RET_PRD_INFO表恢复正常前，不能使用次查询
//			hql = "select count(*) FROM His_ret_prd_info where lot_id = '"+lotId+"' AND cr_ope_id_fk='"+entry.getKey() + "' AND evt_cate='LOGF'";
//			long llogofCnt =(Long) retPrdInfoDao.getCrSession().createQuery(hql).uniqueResult();
//			logofCnt = (int) llogofCnt;
//			opeLogofMap.put(entry.getKey(), logofCnt);
        }
        iter = wipMap.entrySet().iterator();
        List<XpinqlotOB> oaryBList = new ArrayList<XpinqlotOB>();
        while (iter.hasNext()) {
            Entry<StatBoxOpeBnkKey, Integer> wipEntry = (Entry<StatBoxOpeBnkKey, Integer>) iter.next();
            StatBoxOpeBnkKey wipPrd = wipEntry.getKey();
            XpinqlotOB oaryB = new XpinqlotOB();
            List<Ret_prd_info> prdInfoList1 = new ArrayList<Ret_prd_info>();
            oaryB.setBox_id_fk(wipPrd.getBox_id());
            oaryB.setCus_id(cusId);
            oaryB.setOpe_id(wipPrd.getOpe_id());
            oaryB.setPrd_stat(wipPrd.getPrd_stat());
            oaryB.setBnk_flg(wipPrd.getBnk_flg());
            oaryB.setSo_id(soId);
            oaryB.setWo_id(woId);
            oaryB.setMdl_id(mdlId);
            oaryB.setLot_total_cnt(lotTotalCnt);
            oaryB.setWip_cnt(wipEntry.getValue());
            oaryB.setFinished_cnt(finishedCnt);
            oaryB.setUnfinished_cnt(unFinishedCnt);
            oaryB.setOpe_logof_cnt(opeLogofMap.get(wipPrd.getOpe_id()));
            oaryB.setOpe_unlogof_cnt(opeUnLogofMap.get(wipPrd.getOpe_id()));
            oaryB.setOqc_skip_flg(oqcskpflgMap.get(wipPrd));
            if (!("").equals(wipPrd.getBox_id())) {
                prdInfoList1 = retPrdInfoDao.getShtListInfo(wipPrd.getBox_id());
            }
            oaryBList.add(oaryB);
            logger.info(wipEntry.getKey() + ":" + wipEntry.getValue());
        }
        outTrx.setOaryB(oaryBList);
        outTrx.setSum_cnt(oaryBList.size());
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return 0;
    }
}
